﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.ServiceBridge;
using ModelLayer;
using System.ComponentModel;
using AssessmentTool.Services.DTO;
using System.Collections.Generic;
using AssessmentTool.PresentationLogic.EventArguments;
using AssessmentTool.Controls;
using AssessmentTool.Controls.HelperWindow;
using System.Linq;
namespace AssessmentTool.PresentationLogic.Controllers
{
    public class MenuController:AbstractController<Menu>
    {
        bool caricato = false;
        public MenuController(Menu element)
            : base(element)
        {
            ed.subscrive("MENUFIRSTBUILDED", onLogin);
            ed.subscrive("LOGOUT", element.hide);
            ed.subscrive("LOGOUT", unsetCaricato);
            ed.subscrive("OPENLEGEND", openLegend);
            ed.subscrive("ENABLEDISABLEBUTTON", enableButton);
            ed.subscrive("DISABLELEGEND", disableLegend);
            MenuBuilder.getInstance().init(this);
            element.legend.Open.Completed += new EventHandler(LegendOpen_Completed);
        }

        void onLogin(EventArgs e)
        {
            Utente u = (Utente)Session.getInstance().getValue("USER");
            element.setNomeUtente(u.Nome+" "+u.Cognome);
            element.show(e);
        }
        void disableLegend(EventArgs e)
        {
            element.legend.Visibility = Visibility.Collapsed;
        }
        void openLegend(EventArgs e)
        {
            element.legend.Opened = true;
        }
        void LegendOpen_Completed(object sender, EventArgs e)
        {
            if (!caricato)
            {
                caricato = true;
                EventDispatcher.getInstance().dispatch("LOGGED-IN", null);
            }

        }
        void unsetCaricato(EventArgs e)
        {
            caricato = false;
        }

        

        public void addMenuItem(EventArgs e)
        {
            MenuBuildEventArgs arg = (MenuBuildEventArgs)e;
            MenuBottons button = new MenuBottons();
            button.TextIcon = arg.Text;
            button.Image = arg.Icon;
            button.EventName = arg.Event;
            button.First = arg.First;
            button.Name = arg.Text;
            element.addPulsante(button);
        }
        public void enableButton(EventArgs e)
        {
            MenuButtonEventArgs e1 = (MenuButtonEventArgs)e;
            MenuBottons buttonS = (MenuBottons)element.pannelloPulsanti.Children.Where(x => ((MenuBottons)x).Name == e1.Name).Single();
            buttonS.Visibility = e1.Enable?Visibility.Visible:Visibility.Collapsed;
        }
        public void setLegend(IHelper helper)
        {
            element.legend.setHelper(helper);
        }

        public void clear()
        {
            element.clear();
        }
     
        
    }
}
